package com.zoke.daycarpool.adapter;

import java.util.Date;
import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.zoke.daycarpool.R;
import com.zoke.daycarpool.config.ImageConfig;
import com.zoke.daycarpool.config.UrlBank;
import com.zoke.daycarpool.domain.DayCarpool;
import com.zoke.daycarpool.fragment.MainFragment;
import com.zoke.support.base.BaseObjectListAdapter;
import com.zoke.support.utils.CommonUtil;
import com.zoke.support.utils.PersistTool;
import com.zoke.support.utils.TimeUtil;
import com.zoke.support.utils.ViewHolder;
import com.zoke.support.view.ZokeButton;

/**
 * 拼车信息适配器
 * 
 * @author JackWu
 * @version
 * @date 2014-12-30
 * @_QQ_ 651319154
 */
public class CarpoolAdapter extends BaseObjectListAdapter {
	// 如果出发时间与当前时间差值在1个小时以上 则显示已经出发
	public static final long LITMITIME = 60 * 60 * 1000;

	private boolean isTaker;

	public boolean isTaker() {
		return isTaker;
	}

	public void setTaker(boolean isTaker) {
		this.isTaker = isTaker;
	}

	public CarpoolAdapter(Context context, List<? extends Object> datas) {
		super(context, datas);
	}

	public CarpoolAdapter(Context context, List<? extends Object> datas,
			boolean isTaker) {
		super(context, datas);
		this.isTaker = isTaker;
	}

	@Override
	public View getItemView(int position, View convertView, ViewGroup parent) {
		convertView = getConvertView(convertView,
				R.layout.listview_carpool_item);
		final DayCarpool dc = (DayCarpool) mDatas.get(position);
		ZokeButton mPhoneBtn = ViewHolder.get(convertView,
				R.id._carpool_phone_btn);

		ImageView mPaseIv = ViewHolder.get(convertView, R.id._carpool_past_iv);

		ImageView mHeadIv = ViewHolder.get(convertView, R.id._carpool_head_iv);
		TextView mNameTv = ViewHolder.get(convertView, R.id._carpool_uname_tv);
		TextView mStartTimeTv = ViewHolder.get(convertView,
				R.id._carpool_starttime_tv);
		TextView mMainToDisTv = ViewHolder.get(convertView, R.id.main_toDisTv);
		TextView mRemarkTv = ViewHolder.get(convertView, R.id._carpool_ps_tv);
		TextView mSeatTv = ViewHolder.get(convertView, R.id._carpool_seat_tv);
		TextView mPubTimeTv = ViewHolder
				.get(convertView, R.id._carpool_time_tv);
		TextView mStartAdrTv = ViewHolder.get(convertView,
				R.id._carpool_start_tv);
		TextView mDestAdrTv = ViewHolder.get(convertView, R.id._carpool_end_tv);
		TextView mPriceTv = ViewHolder.get(convertView, R.id.main_priceTv);
		ImageLoader.getInstance().displayImage(UrlBank.httpurl + dc.getPhoto(),
				mHeadIv, ImageConfig.getHeadOptions());
		mStartTimeTv.setText("出发时间:" + dc.getStartLit());
		mRemarkTv.setText("备注:" + dc.getRemark());
		mRemarkTv.setVisibility(TextUtils.isEmpty(dc.getRemark()) ? View.GONE
				: View.VISIBLE);
		mStartAdrTv.setText(dc.getStart());
		mDestAdrTv.setText(dc.getDest());
		String str = TimeUtil.getTime(dc.getPubTime());
		if (!TextUtils.isEmpty(dc.getDist()) && !dc.getDist().equals("0")
				&& Double.parseDouble(dc.getDist()) != 0) {
			str = str + " | " + dc.getDist() + "km";
		}
		mPubTimeTv.setText(str);
		int target = dc.getTarget();
		String tdForStart = PersistTool.getString(
				MainFragment.ChannelNameForStart, "去北京");
		String tdForenD = PersistTool.getString(MainFragment.ChannelNameForEnd,
				"去香河");
		String toTarstr = target == 1 ? tdForStart : tdForenD;
		long dateNow = new Date().getTime();
		mPaseIv.setVisibility((dateNow - dc.getStartTime() >= LITMITIME) ? View.VISIBLE
				: View.GONE);
		// 如果是乘客
		if (isTaker) {
			mSeatTv.setText("座位:" + dc.getNum());
			mSeatTv.setVisibility(dc.getNum() == 0 ? View.GONE : View.VISIBLE);
			mNameTv.setText(dc.getName() + "  (车找人)");
			mMainToDisTv.setVisibility(View.VISIBLE);
			mPriceTv.setText(dc.getPrice() + "元/人");
			mMainToDisTv.setText(toTarstr);
		} else {
			mNameTv.setText(dc.getName() + "  (人找车)");
			mMainToDisTv.setVisibility(View.GONE);
			mPriceTv.setText(toTarstr);
			mSeatTv.setText("人数:" + dc.getNum());
			mSeatTv.setVisibility(dc.getNum() == 0 ? View.GONE : View.VISIBLE);
		}

		mPhoneBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				CommonUtil.callPhone(dc.getMobile(), mContext);
			}
		});
		return convertView;
	}
}
